// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions::sparksql::test {
namespace {

class JsonObjectKeysTest : public SparkFunctionBaseTest {
 protected:
  VectorPtr jsonObjectKeys(const std::string& json) {
    auto varcharVector = make_flat_vector<std::string>({json});
    return evaluate("json_object_keys(c0)", make_row_vector({varcharVector}));
  }
};

TEST_F(JsonObjectKeysTest, basic) {
  auto expected =
      make_array_vector_from_json<std::string>({"[\"name\",\"age\",\"id\"]"});
  assertEqualVectors(
      jsonObjectKeys(R"({"name": "Alice", "age": 5, "id": "001"})"), expected);

  expected = make_array_vector_from_json<std::string>({"[]"});
  assertEqualVectors(jsonObjectKeys(R"({})"), expected);

  expected = make_nullable_array_vector<std::string>({std::nullopt});
  assertEqualVectors(jsonObjectKeys(R"(1)"), expected);
  assertEqualVectors(jsonObjectKeys(R"("hello")"), expected);
  assertEqualVectors(jsonObjectKeys(R"(invalid json)"), expected);
  assertEqualVectors(jsonObjectKeys(R"("")"), expected);
  assertEqualVectors(
      jsonObjectKeys(R"({"key": 45, "random_string"})"), expected);
  assertEqualVectors(jsonObjectKeys(R"([{1, 2, 3}])"), expected);
  // Test with UNCLOSED_STRING error of simdjson.
  assertEqualVectors(jsonObjectKeys(R"({"key: 45})"), expected);
  assertEqualVectors(
      jsonObjectKeys(R"({ "pie": true, "cherry": [1, 2, 3 })"), expected);
  assertEqualVectors(jsonObjectKeys(R"([*.{cs,vb}])"), expected);
}

} // namespace
} // namespace kumo::pollux::functions::sparksql::test
